Flash 5 - Part 6: Basic Action-scripting

TAD

Introduction

As far as I know Flash didn't have any action-scripting until Flash 3 and that was very basic VCR style controls (Stop, Play, Goto). This seems like a good place to start with our Flash 5 action-scripting, after all, most of the time you will be using these actions to control your various movie-clip animations.

Act first, Draw later

When Flash processes the current frame it executes all the action-scripting first and then renders all the graphics (Instances, Text fields, polygon shapes etc..). This gives you chance to modify the appearance of Movie-Clip instances through action-scripting before anything is drawn.

Action-scripting?

If you already know JavaScript (or even JScript, spit, spit) you already know Flash Action-scripting (or 'as' for short) because the language is very similiar indeed. Which IMVHO is a good thing when you're trying to create web based stuff and already have to deal with a load of different languages, interfaces, web browsers version etc...)

If you're familiar with other programming languages then learning action-scripting will be fairly painless because it has two levels, Normal and Expert mode. I personally use Expert mode, not cause I think I'm l33t, but because you can type the scripting directly into the window rather than having to use the painfully slow menu and sub-menu system.

Tip: Click on the small, top-right > arrow icon and choose Expert-Mode, then use the Esc+XX abbreviations to quickly enter actions. You can learn the abbreviations by looking at the + drop down menu. Also drag the vertical bar all the way to the left to give yourself a much bigger action window.

Instances and Targets

Before we can make Movie-Clips do some funky things we need to create some symbols (lazy readers are now directed to the examples in the supplied ZIP file ;-).

Once we have some Symbols (ideally Movie-clip which contains a simple animation and a Button) we can drag a couple of Instances onto the main stage work-area. So open the Library window, drag a copy of the Movie-Clip animation and 2 copies of the Buttons. Select the Movie-Clip and give it the Instance Name of "mcAnim". Now right-click on the 1st button Instance and give it the following actions:

on (release) {
    tellTarget ("mcAnim") {
        stop ();
    }
}

This basically creates a small event handler attached to the 1st button. Whenever the mouse button is pressed and released over the Button Hit area it will send a message to our Movie-Clip animation Instance called (yep) 'mcAnim' and tell it to stop playing.

Now right-click and add these actions to the 2nd button Instance.

on (release) {
    tellTarget ("mcAnim") {
        play ();
    }
}

This will obviously tell the 'mcAnim' Instance to begin playing again.

Levels, Paths and Targets

The previous targets (ie. the paths to a particular Instance name) were very easy because they were in the same Movie-Clip container. But things get far more tricky when dealing with Instances inside other Movie-Clip containers. It is no more difficult than using sub-directories/folders, you can either have a Relative path (starting from the current location) or an Absolute path (starting from the very root).

Say for example we have a computer inside a room inside a building inside a city, then we can access the various parts using a Target string. The button is placed inside the outer root Movie so it can access the computer Instance like this:

on (release) {
	tellTarget ("mcCity/mcBuilding/mcRoom/mcComputer") {
		play ();
	}
}

If our button (and it's attached action-script) was placed inside the Room Movie-clip then we can do this

	tellTarget ("mcComputer") {
		play ();
	}
}

Or, we could use an absolute path such as:

	tellTarget ("/mcCity/mcBuilding/mcRoom/mcComputer") {

Levels:

By default the root Flash Movie is loaded into _level0 so you could use this in the target path:

	tellTarget ("_level0/mcCity/mcBuilding/mcRoom/mcComputer") {

You can load other Flash movies into the current one by using the loadMovieNum() function and specifying a _level to load it into. You can then pass values between these independant movies using the _level0, _level1 .. etc.. target prefixes.

OOPS and Targets

As you may already know Flash 5 is now more Object-Orientated in terms of its programming language. It does actually make action-scripting much easier when dealing with Movie-Clips. Instead of the rather messy (but still, often needed) '/' slash notation for target paths it can use '.' dot notation. It also means you have direct access to all the Method and Properties of each object.

My advice is to use the '.' dot notation as much as possible you will save yourself far more typing ;-)

on (release) {
	_root.mcCity.mcBuilding.mcRoom.mcComputer.play();
}

You can also backtrack and access the parent Movie-Clip container if you wanted:

on (release) {
	_parent.mcLake.mcBoat.gotoAndStop(4);
}

Sounds and Time-lines

You might be wondering when TAD is going to get around to introduce sound into Flash, well, right here ;-)

To use a sound (ie. WAV file) inside Flash (don't worry, it will be packed as a MP3 when you publish the movie) you must first Import it into the Library. Once in the Library, create a new blank Movie-Clip container, Insert a stop() action on the very 1st frame, Insert Blank KeyFrame and open the Sound panel.

On frame 2, select the Imported sound filename from the above drop-down menu. You should see a rough waveform image on the time-line. This means the sound will play when Flash reaches frame 2 on this time-line.

Why frame 2, huh?

Now, return back to the main Scene work-area and drag an Instance of that Movie-Clip container. Give the Instance a name (eg. "mcSound") and also drag an Instance of a Button (you can always open up the example Flash Movie and drag a Button from that if you are too lazy to create a button yourself).

So we have an Instance of our 'sound Movie-clip' and an Instance of a Button, let's connect them using some action-scripting.

on (release) {
	mcSound.gotoAndPlay(2);
}

You should be able test the movie by hitting [CTRL-ENTER] or Control --> Test Movie from the menu. When you press and release the mouse button over the Button you should be able to hear a sound. If we placed the sound at frame 1, then it would play automatically. By using frame 2 we can control it using a simple gotoAndPlay(2) action to trigger the sound.

Closing words

As I hope you have seen creating stuff using action-scripting and Flash is pretty easy once you understand how the MacroMedia designers think. There are plenty of good websites on the net if you want to learn about Flash and get into some advanced action-scripting. Flash mx (or Flash 6) has some interesting new actions which were previously missing from Flash 5 they include line and polygon drawing statements. So I wonder who will be first with a nice kick-ass 3d engine written entirely in action-script?

Keep on Coding!

TAD